
package sw6.visualschedule;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Date;

public class ActivitiesOfTheDay extends Activity {

    private LinearLayout mCheckboxContainer;
    private final ArrayList<CalendarEntry> mEntries = new ArrayList<CalendarEntry>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitiesoftheday);

        mCheckboxContainer = (LinearLayout) findViewById(R.id.checkboxContainer);

        fillData();

        for (CalendarEntry entry : mEntries) {

            CheckBox checkBox = new CheckBox(this);
            // checkBox.setId(1);
            checkBox.setText(entry.getTitle());
            checkBox.setChecked(entry.isEnabled());

            mCheckboxContainer.addView(checkBox);
        }

        Button mSaveButton = (Button) findViewById(R.id.saveButton);
        mSaveButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                saveData();
            }

        });
        
        Button cancelButton = (Button)findViewById(R.id.cancelButton);
        cancelButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                ActivitiesOfTheDay.this.finish();
            }
        });
    }

    private void saveData() {
        int checkedCount = 0;

        for (int i = 0; i < mCheckboxContainer.getChildCount(); i++) {
            CheckBox checkBox = (CheckBox)
                     mCheckboxContainer.getChildAt(i);
            if (checkBox.isChecked())
                checkedCount++;
        }
        
        String activities = null;
        if(checkedCount == 1) activities = getResources().getString(R.string.activitiesOfTheDay_activity);
        else activities = getResources().getString(R.string.activitiesOfTheDay_activities);
        
        Toast.makeText(ActivitiesOfTheDay.this, getResources().getString(R.string.activitiesOfTheDay_chosen, checkedCount, activities), Toast.LENGTH_SHORT).show();
        
        finish();
    }

    private void fillData() {
        CalendarEntry e1 = new CalendarEntry();
        e1.setDateTime(new Date(2011, 3, 1, 8, 0));
        e1.setEnabled(true);
        e1.setTitle("Stå op");

        CalendarEntry e2 = new CalendarEntry();
        e2.setDateTime(new Date(2011, 3, 1, 8, 5));
        e2.setEnabled(true);
        e2.setTitle("Børst tænder");

        mEntries.add(e1);
        mEntries.add(e2);
    }
}
